///PHILOTES Source Code.  (C)2012 PhiloLabs
#pragma once

#ifndef __CompositionPass_H__
#define __CompositionPass_H__

#include "OgrePrerequisites.h"
#include "OgreMaterial.h"
#include "OgreRenderSystem.h"
#include "OgreRenderQueue.h"

namespace Ogre {
	
	
	
	class _OgreExport CompositionPass : public MemAlloc_Resource
    {
    public:
        CompositionPass(CompositionTargetPass *parent);
        ~CompositionPass();
        
        
        enum PassType
        {
            PT_CLEAR,           // Clear target to one colour
			PT_STENCIL,			// Set stencil operation
            PT_RENDERSCENE,     // Render the scene or part of it
            PT_RENDERQUAD,      // Render a full screen quad
			PT_RENDERCUSTOM		// Render a custom sequence
        };
        
        
        void setType(PassType type);
        
        PassType getType() const;
        
		
		void setIdentifier(uint32 id);
		
		uint32 getIdentifier() const;

        
        void setMaterial(const MaterialPtr& mat);
        
        void setMaterialName(const String &name);
        
        const MaterialPtr& getMaterial() const;
		
        void setFirstRenderQueue(uint8 id);
		
		uint8 getFirstRenderQueue() const;
		
        void setLastRenderQueue(uint8 id);
		
		uint8 getLastRenderQueue() const;

		
		void setMaterialScheme(const String& schemeName);
		
		const String& getMaterialScheme(void) const;

		

        
        void setClearBuffers(uint32 val);
        
        uint32 getClearBuffers() const;
        
        void setClearColour(ColourValue val);
        
        const ColourValue &getClearColour() const;
        
        void setClearDepth(Real depth);
        
        Real getClearDepth() const;
		
        void setClearStencil(uint32 value);
        
        uint32 getClearStencil() const;

		
		void setStencilCheck(bool value);
		
		bool getStencilCheck() const;
		
		void setStencilFunc(CompareFunction value); 
		
		CompareFunction getStencilFunc() const; 
		
		void setStencilRefValue(uint32 value);
		
		uint32 getStencilRefValue() const;
		
		void setStencilMask(uint32 value);
		
		uint32 getStencilMask() const;
		
		void setStencilFailOp(StencilOperation value);
		
		StencilOperation getStencilFailOp() const;
		
		void setStencilDepthFailOp(StencilOperation value);
		
		StencilOperation getStencilDepthFailOp() const;
		
		void setStencilPassOp(StencilOperation value);
		
		StencilOperation getStencilPassOp() const;
		
		void setStencilTwoSidedOperation(bool value);
		
		bool getStencilTwoSidedOperation() const;

		/// Inputs (for material used for rendering the quad)
		struct InputTex
		{
			/// Name (local) of the input texture (empty == no input)
			String name;
			/// MRT surface index if applicable
			size_t mrtIndex;
			InputTex() : name(StringUtil::BLANK), mrtIndex(0) {}
			InputTex(const String& _name, size_t _mrtIndex = 0)
				: name(_name), mrtIndex(_mrtIndex) {}
		};

        
        void setInput(size_t id, const String &input=StringUtil::BLANK, size_t mrtIndex=0);
        
        
        const InputTex &getInput(size_t id) const;
        
        
        size_t getNumInputs() const;
        
        
        void clearAllInputs();
        
        
        CompositionTargetPass *getParent();

        
        bool _isSupported(void);

        
        void setQuadCorners(Real left,Real top,Real right,Real bottom);

        
        bool getQuadCorners(Real & left,Real & top,Real & right,Real & bottom) const;
            
		
		void setQuadFarCorners(bool farCorners, bool farCornersViewSpace);

		
		bool getQuadFarCorners() const;

		
		bool getQuadFarCornersViewSpace() const;

		
		void setCustomType(const String& customType);

		
		const String& getCustomType() const;

    private:
        /// Parent technique
        CompositionTargetPass *mParent;
        /// Type of composition pass
        PassType mType;
		/// Identifier for this pass
		uint32 mIdentifier;
        /// Material used for rendering
        MaterialPtr mMaterial;
        /// [first,last] render queue to render this pass (in case of PT_RENDERSCENE)
		uint8 mFirstRenderQueue;
		uint8 mLastRenderQueue;
		/// Material scheme name
		String mMaterialScheme;
        /// Clear buffers (in case of PT_CLEAR)
        uint32 mClearBuffers;
        /// Clear colour (in case of PT_CLEAR)
        ColourValue mClearColour;
		/// Clear depth (in case of PT_CLEAR)
		Real mClearDepth;
		/// Clear stencil value (in case of PT_CLEAR)
		uint32 mClearStencil;
        /// Inputs (for material used for rendering the quad)
        /// An empty string signifies that no input is used
        InputTex mInputs[OGRE_MAX_TEXTURE_LAYERS];
		/// Stencil operation parameters
		bool mStencilCheck;
		CompareFunction mStencilFunc; 
		uint32 mStencilRefValue;
		uint32 mStencilMask;
		StencilOperation mStencilFailOp;
		StencilOperation mStencilDepthFailOp;
		StencilOperation mStencilPassOp;
		bool mStencilTwoSidedOperation;

        /// true if quad should not cover whole screen
        bool mQuadCornerModified;
        /// quad positions in normalised coordinates [-1;1]x[-1;1] (in case of PT_RENDERQUAD)
        Real mQuadLeft;
        Real mQuadTop;
        Real mQuadRight;
        Real mQuadBottom;

		bool mQuadFarCorners, mQuadFarCornersViewSpace;
		//The type name of the custom composition pass.
		String mCustomType;
    };
	
	

}

#endif
